using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using SamasDBI;
using SamasDBI.MySQL;
using EQEmuMysql;

namespace EQEmulatorGUI
{
    public partial class frmPetitions : Form
    {
        private SortedList<int, Petitions> lsPetitions;
        private Petitions objPetition;
        private bool flgPetitionsLoaded = false;
        public frmPetitions()
        {
            InitializeComponent();
        }
        public void PetitionsList()
        {
            flgPetitionsLoaded = false;
            ListViewItem lsvItem;
            lsvPetitions.Items.Clear();

            if (flgPetitionsLoaded == false)
            {
                lsPetitions = Lists.PetitionList();
                flgPetitionsLoaded = false;
            }
            foreach (Petitions oPetition in lsPetitions.Values)
            {
                lsvItem = new ListViewItem(oPetition.ID.ToString());
                lsvItem.Tag = oPetition.ID.ToString();

                lsvPetitions.Items.Add(lsvItem);
            }
        }

        private void frmPetitions_Load(object sender, EventArgs e)
        {
            PetitionsList();
        }

        private void lsvPetitions_DoubleClick(object sender, EventArgs e)
        {
            if (lsvPetitions.SelectedItems.Count <= 0)
            {
                return;
            }

            objPetition = lsPetitions[int.Parse(lsvPetitions.SelectedItems[0].Tag.ToString())];

            txtCharName.Text = objPetition.CharName;
            txtAccountName.Text = objPetition.AccountName;
            txtLastGM.Text = objPetition.LastGM;
            txtZone.Text = objPetition.Zone;
            txtUrgency.Text = objPetition.Urgency;
            txtCharClass.Text = objPetition.CharClass;
            txtCharRace.Text = objPetition.CharRace;
            txtCharLevel.Text = objPetition.CharLevel;
            txtCheckOuts.Text = objPetition.CheckOuts;
            txtUnavailables.Text = objPetition.Unavailables;
            txtIsCheckedOut.Text = objPetition.IsCheckedOut;
            txtSentTime.Text = objPetition.SentTime;
            txtPetitionText.Text = objPetition.PetitionText;
            txtGMText.Text = objPetition.GMText;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            objPetition.CharName = txtCharName.Text;
            objPetition.AccountName = txtAccountName.Text;
            objPetition.LastGM = txtLastGM.Text;
            objPetition.Zone = txtZone.Text;
            objPetition.Urgency = txtUrgency.Text;
            objPetition.CharClass = txtCharClass.Text;
            objPetition.CharRace = txtCharRace.Text;
            objPetition.CharLevel = txtCharLevel.Text;
            objPetition.CheckOuts = txtCheckOuts.Text;
            objPetition.Unavailables = txtUnavailables.Text;
            objPetition.IsCheckedOut = txtIsCheckedOut.Text;
            objPetition.SentTime = txtSentTime.Text;
            objPetition.PetitionText = txtPetitionText.Text;
            objPetition.GMText = txtGMText.Text;

            if (objPetition.Save() == true)
            {
                MessageBox.Show("Update Successfull.", "Update Petition");
                ClearPetition();
                PetitionsList();
            }
            else
                MessageBox.Show("Update Failed.", "Update Petition");
        }
        private void ClearPetition(){
            txtCharName.Text = "";
            txtAccountName.Text = "";
            txtLastGM.Text = "";
            txtZone.Text = "";
            txtUrgency.Text = "";
            txtCharClass.Text = "";
            txtCharRace.Text = "";
            txtCharLevel.Text = "";
            txtCheckOuts.Text = "";
            txtUnavailables.Text = "";
            txtIsCheckedOut.Text = "";
            txtSentTime.Text = "";
            txtPetitionText.Text = "";
            txtGMText.Text = "";
        }
    }
}